<?php
/**
 * @author GhostWei
 * @date Sep 16, 2012
 */
class community_member_avatar_concept extends system_concept_abstract {
	
	public function init() {
		$this->setModel('community_member_avatar_model', 'avatar');
		$this->setModelCols('avatar', array('passport_id', 'hash', 'creationip', 'creationtime'));		
	}

	public function save($row) {
		if(!($passport = system_passport_instance::getRow($row->getPassportId()))) {
			throw new community_exception("passport can not be empty");
		}
		if(!preg_match('/^[a-z0-9]{32}$/i', $row->getHash())) {
			throw new community_exception("avatar hash formation error");
		}
		if(!$this->fetch(array('passport_id = ?' => $passport->getId(), 'hash = ?' => $row->getHash()))) {
			$row->setCreationtime(system_date::timestamp());
			$this->getModel('avatar')->insert($row->getModelData('avatar'));
		}
		return $row;
	}
	
	public function delete($row) {
		return;
	}
	
	protected $_avatar = null;
	/**
	 * @return resource_display
	 */
	public function getAvatar($row) {
		if($this->_avatar === null || $this->_avatar->getHash() != $row->get('hash')) {
			$this->_avatar = new resource_display($row->get('hash'), 'avatar');
		}
		return $this->_avatar;
	}
	
}
